/*
Copyright (c) 2010 Bill Davidsen (wdavidsen@yahoo.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.ComponentModel;
using System.Windows.Forms.Design;
using System.Collections;

namespace SCS.Web.UI.WebControls.Design
{
	public abstract class ListboxTypeEditor : UITypeEditor
	{
		private IWindowsFormsEditorService formEditorService = null;
		
		protected abstract void FillInList(ITypeDescriptorContext context, IServiceProvider provider, ListBox listBox);

		/// <summary>
		/// Create a drop down list box.
		/// </summary>
		/// <param name="context"></param>
		/// <returns></returns>
		public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
		{
			if (context != null && context.Instance != null) 
			{
				return UITypeEditorEditStyle.DropDown;
			}
			return base.GetEditStyle(context);
		}
		
		/// <summary>
		/// Create the ListBox control for the property grid.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="provider"></param>
		/// <param name="newValue"></param>
		/// <returns></returns>
		public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object newValue)
		{
			if (context != null && context.Instance != null	&& provider != null) 
				
				try
				{
					// get the editor service
					this.formEditorService = 
						(IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

					// create the control for the UI
					ListBox listBox = new ListBox();
					listBox.Click += new EventHandler(List_Click);
            
					// modify the control properties
					FillInList(context, provider, listBox);

					// set the list selection
					listBox.SelectedItem = newValue;
         
					// editor service renders control and manages control events
					this.formEditorService.DropDownControl(listBox);
   
					// return the updated newValue
					return listBox.SelectedItem;
				} 
				finally
				{
					this.formEditorService = null;
				}
			else
				return newValue;

		} 
		/// <summary>
		/// Handle click event.
		/// </summary>
		/// <param name="pSender"></param>
		/// <param name="pArgs"></param>
		protected void List_Click(object sender, EventArgs args)
		{
			this.formEditorService.CloseDropDown();
		} 
	}
}
